package com.sharecode.stock.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.sharecode.stock.vo.DateVo;

public class DateUtils {
	public static final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
	public static final String FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	public static String format(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS);
		return sdf.format(date);
	}

	public static String format(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static String format(Long l) {
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS);
		return sdf.format(new Date(l));
	}

	public static String format(Long l, String format) {
		return new SimpleDateFormat(format).format(new Date(l));
	}

	public static Date parse(String parse) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS);
			return sdf.parse(parse);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date parse(String parse, String format) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(parse);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String format(String l) {
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS);
		return sdf.format(new Date(Long.parseLong(l)));
	}

	public static String format() {
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS);
		return sdf.format(new Date());
	}

	public static String month(int next, String format) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, next);
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}

	public static String day(int next, String format) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DATE, next);
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}
	public static String day(int next, String date, String format) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parse(date, format));
		calendar.add(Calendar.DATE, next);
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}

	public static DateVo day(int next, Date date, String format) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, next);
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return new DateVo(calendar.get(Calendar.DAY_OF_WEEK), sdf.format(calendar.getTime()));
	}

	// 星期日：1 星期一：2
	public static int week() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	public static void main(String[] args) {
		Date date = parse("2018-01-01", FORMAT_YYYY_MM_DD);
		List<DateVo> dateVos = new ArrayList<>();
		for (int i = 0; i < 3000; i++) {
			DateVo vo = day(i, date, FORMAT_YYYY_MM_DD);
			int week = vo.getWeek();
			if (week == 1 || week == 2) {
				System.out.println(vo.getDate());
				dateVos.add(vo);
			}
		}
		System.out.println(dateVos.size());
	}
}
